﻿namespace gRPCDemo.CommonModule
{
    public class Client: BLL.ICommonModuleClient
    {
        /// <summary>
        /// 使用CommonModule目录下的Moduleinf.proto
        /// </summary>
        /// <returns></returns>
        public async Task CommonModuleTest()
        {
            var channel = GrpcChannel.ForAddress("http://guo-b760-3060:5577");
            var client = new Module.ModuleClient(channel);

            var jdoc = JsonDocument.Parse(File.Open("plugins/grpc/gRPCDemo.json", FileMode.Open), new JsonDocumentOptions
            {
                AllowTrailingCommas = true,
                CommentHandling = JsonCommentHandling.Skip
            });
            List<FunctionRequest> requests = [];
            foreach (var item in jdoc.RootElement.GetProperty("grpcRequests").EnumerateArray())
            {
                if (!item.GetProperty("enable").GetBoolean())
                {
                    continue;
                }
                var request = new FunctionRequest
                {
                    Command = item.GetProperty("command").GetString(),
                };
                foreach (var item1 in item.GetProperty("parameters").EnumerateObject())
                {
                    request.Parameter.Add(item1.Name, Google.Protobuf.ByteString.CopyFromUtf8(item1.Value.GetString()));
                }
                requests.Add(request);
            }
            foreach (var request in requests)
            {
                Console.WriteLine("\n执行功能：{0}。", request.Command);
                try
                {
                    var result = await client.functionAsync(request);
                    if (result.Parameter.Count == 0)
                    {
                        Console.WriteLine("无返回数据");
                    }
                    foreach (var item in result.Parameter)
                    {
                        Console.WriteLine("{0}:{1}", item.Key, item.Value.ToStringUtf8());
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("执行异常！{0}", e);
                }
            }



            var settingsRequest = new SettingsRequest
            {
                Type = SettingsRequest.Types.ReqType.Write,
            };
            settingsRequest.Parameter.Add("moduelName", Google.Protobuf.ByteString.CopyFromUtf8("HikIsapi"));
            var settingsResult = await client.settingsAsync(settingsRequest);
            Console.WriteLine("\n执行功能：{0}。", settingsRequest.Type);
            if (settingsResult.Parameter.Count == 0)
            {
                Console.WriteLine("无返回数据");
            }
            foreach (var item in settingsResult.Parameter)
            {
                Console.WriteLine("{0}:{1}", item.Key, item.Value.ToStringUtf8());
            }


            settingsRequest = new SettingsRequest
            {
                Type = SettingsRequest.Types.ReqType.Read,
            };
            settingsResult = await client.settingsAsync(settingsRequest);
            Console.WriteLine("\n执行功能：{0}。", settingsRequest.Type);
            if (settingsResult.Parameter.Count == 0)
            {
                Console.WriteLine("无返回数据");
            }
            foreach (var item in settingsResult.Parameter)
            {
                Console.WriteLine("{0}:{1}", item.Key, item.Value.ToStringUtf8());
            }

            var rp = client.signal(new Google.Protobuf.WellKnownTypes.Empty());
            while (await rp.ResponseStream.MoveNext())
            {
                var data = rp.ResponseStream.Current;
                Console.WriteLine(data.Name);
                Console.WriteLine(data.Time.ToDateTime().ToString());
                foreach (var item in data.Parameter)
                {
                    Console.WriteLine("参数——{0}:{1}", item.Key, item.Value.ToStringUtf8());
                }
                Console.WriteLine("**************");
            }

        }
    }
}
